﻿using HY.Common;
using HY.Entity;
using HY.Service;
using HY.Web.Controllers;
using HY.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HY.Web.Areas.Stock.Controllers
{
    public class StockInfoController : BaseController
    {
        BaseService service = new BaseService();
        // GET: Stock/StockInfo
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult List(int page, int limit)
        {
            ReturnModel returnModel = new ReturnModel();
            try
            {
                var name = Request.Form["Name"];
                var where = "SI.IsDeleted=0";
                if (!string.IsNullOrWhiteSpace(name))
                    where += $" AND S.Name like '%{name}%'";
                var tuple = service.GetPageList<V_StockInfoList>("StockInfo SI JOIN Stock S ON SI.StockID=S.ID JOIN SysDept SD ON SI.DeptID=SD.ID JOIN Park P ON SI.ParkID=P.ID JOIN [Location] L ON SI.LocationID=L.ID JOIN Storehouse SH ON SI.StorehouseID=SH.ID", "SI.Number,S.Name StockName,S.Code StockCode,S.Type StockType,S.Unit,S.Weight UnitWeight,SI.Number*S.Weight TotalWeight,SD.DeptCode,SD.DeptName,P.Code ParkCode,SH.Code StorehouseCode,L.Code LocationCode,SI.CreateDate", where, page, limit, "SI.CreateDate desc");
                returnModel = base.GetReturnModel(tuple.Item1, tuple.Item2);
            }
            catch (Exception ex)
            {
                returnModel = base.GetReturnModel(ex);
                LogHelper.Error(new LogInfo() { UserID = UserInfo.UserID, LogType = LogType.Select, LogMessage = "查询库存列表" }, ex);
            }
            return Json(returnModel);
        }
    }
}